/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.client.ui;

import java.util.HashMap;
import java.util.Map;

import cacao.client.CaCaO;
import cacao.client.data.ItemDetailsObject;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.DockPanel.DockLayoutConstant;


public class TopMenuCmp extends Composite  implements CaCaOComposite, ClickHandler {
	private CaCaO application;
	private HTML lblCardID;
	@SuppressWarnings("unused")
	private PushButton btnRWD50, btnRWD25, btnRWD10, btnRWD05, btnRWD;
	@SuppressWarnings("unused")
	private PushButton btnFWD50, btnFWD25, btnFWD10, btnFWD05, btnFWD;
	private DockLayoutPanel panel;
	private Map<PushButton, Integer> turnSteps;

	public TopMenuCmp(CaCaO app){
		application=app;		
		turnSteps = new HashMap<PushButton, Integer>();
		panel = new DockLayoutPanel(Unit.PX);
		panel.setStyleName("panel-top");
		//panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);

		btnRWD50 = createNavigationButton(panel, application.getIconImageBundle().rwnd50Icon(), DockPanel.WEST, -50);
		btnRWD25 = createNavigationButton(panel, application.getIconImageBundle().rwnd25Icon(), DockPanel.WEST, -25);
		btnRWD10 = createNavigationButton(panel, application.getIconImageBundle().rwnd10Icon(), DockPanel.WEST, -10);
		btnRWD05 = createNavigationButton(panel, application.getIconImageBundle().rwnd05Icon(), DockPanel.WEST,  -5);
		btnRWD = createNavigationButton(panel, application.getIconImageBundle().previousIcon(), DockPanel.WEST,  -1);
		btnFWD50 = createNavigationButton(panel, application.getIconImageBundle().ffwd50Icon(), DockPanel.EAST, +50);
		btnFWD25 = createNavigationButton(panel, application.getIconImageBundle().ffwd25Icon(), DockPanel.EAST, +25);
		btnFWD10 = createNavigationButton(panel, application.getIconImageBundle().ffwd10Icon(), DockPanel.EAST, +10);
		btnFWD05 = createNavigationButton(panel, application.getIconImageBundle().ffwd05Icon(), DockPanel.EAST,  +5);
		btnFWD = createNavigationButton(panel, application.getIconImageBundle().nextIcon(),     DockPanel.EAST,  +1);

		lblCardID = new HTML();
		lblCardID.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		//	lblCardID.setHeight("100%");
		lblCardID.setText(" ");
		lblCardID.setStylePrimaryName("lbl-imageNavigationLabel");

		panel.add(lblCardID);

		initWidget(panel);
	}

	private PushButton createNavigationButton(DockLayoutPanel panel, ImageResource imgRes, DockLayoutConstant layout, int steps){
		PushButton btn  = new PushButton(new Image(imgRes));
		btn.setStylePrimaryName("btn-imageNavigation");
		btn.setTitle(application.getMessages().navigate()+" "+steps);
		if(layout.equals(DockPanel.WEST)){
			panel.addWest(btn, 50);
		}
		if(layout.equals(DockPanel.EAST)){
			panel.addEast(btn, 50);
		}

		turnSteps.put(btn, steps);
		btn.addClickHandler(this);
		return btn;
	}

	public void updateUI(){
		ItemDetailsObject currentItem = application.getCurrentItem();	
		if(currentItem!=null){
			String label = "<a class='lbl-imgNavigationLabelText' href='"+application.getProperties().get("CaCaO.client.application.url")+"?card="+currentItem.getId()+"'>"+currentItem.getId()+"</a>";
			String text = "<table width='100%' height='100%'><tr><td valign='middle' align='center'>"+label+"</td></tr></table>";
			lblCardID.setHTML(text);
		}
		else{
			lblCardID.setText(" ");
		}
	}

	public void onClick(ClickEvent evt) {
		Integer x = turnSteps.get(evt.getSource());
		if(x!=null){
			application.turnPage(x);
		}
	}
}
